//
//  SplashViewController.m
//  HappyBug
//
//  Created by liulei on 2018/10/16.
//  Copyright © 2018年 www.hc.com.cn. All rights reserved.
//

#import "SplashViewController.h"
#import "UIMainBarViewController.h"
#import "AppDelegate.h"
#import "NewsViewController.h"
#import "ReadingViewController.h"
#import "MineViewController.h"
#import "NewsViewController.h"
#import "ReadingViewController.h"
#import "MineViewController.h"
#import "MessageViewControler.h"
#import "MyTableViewController.h"

#define  HEIGHT   [UIScreen mainScreen].bounds.size.height
#define  WITH     [UIScreen mainScreen].bounds.size.width
@interface SplashViewController ()



@end

@implementation SplashViewController{
    NSUserDefaults *userdefalt;
    NSString *isFirst;
    AppDelegate *app;
    UIMainBarViewController *barController;
    UIScrollView *scrollview;
    
    NewsViewController *newsCV;
    ReadingViewController *readCV;
    MineViewController *mineCV;
    
    UINavigationController *newsNaCV;
    UINavigationController *readNaCV;
    MyTableViewController *tabview;
    UINavigationController *mineNaCV;
    UINavigationController *navtabview;
    MessageViewControler *messageCV;
    UIPageControl *pageControl;
    
    
}


-(void)initCV{
    newsCV =[[NewsViewController alloc]init];
    readCV =[[ReadingViewController alloc]init];
    mineCV=[[MineViewController alloc]init];
    newsCV.tabBarItem.title=@"新闻";
    newsCV.tabBarItem.image=[UIImage imageNamed:@"tabbar_icon_news_normal"];
    
    
    
    tabview =[[MyTableViewController alloc]init];
    tabview.tabBarItem.title=@"Tab";
    tabview.tabBarItem.image=[UIImage imageNamed:@"tabbar_icon_news_normal"];
    navtabview=[[UINavigationController alloc]initWithRootViewController:tabview];

                             
    readCV.tabBarItem.title=@"阅读";
    readCV.tabBarItem.image=[UIImage imageNamed:@"tabbar_icon_reader_normal"];
    
    mineCV.tabBarItem.title=@"我";
    mineCV.tabBarItem.image=[UIImage imageNamed:@"tabbar_icon_me_normal"];
    
    newsNaCV=[[UINavigationController alloc]initWithRootViewController:newsCV];
    readNaCV=[[UINavigationController alloc]initWithRootViewController:readCV];
    
    messageCV=[[MessageViewControler alloc]init];
    messageCV.tabBarItem.title=@"消息";
    messageCV.tabBarItem.image=[UIImage imageNamed:@"tabbar_icon_news_normal"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCV];
    [self initData];
    
}
-(void)initData{
    userdefalt=[NSUserDefaults standardUserDefaults];
    NSString *isFistApp= [userdefalt objectForKey:@"isFirst"];
    if([isFistApp isEqualToString:@"1"]){
        [self enterMainControler];
    }else{
        [self  openSplash];
        
    }
    
}

#pragma mark - scrollView的代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //计算pagecontroll相应地页(滚动视图可以滚动的总宽度/单个滚动视图的宽度=滚动视图的页数)
    NSInteger currentPage = (int)(scrollView.contentOffset.x) / (int)(self.view.frame.size.width);
    pageControl.currentPage = currentPage;//将上述的滚动视图页数重新赋给当前视图页数,进行分页
}
-(void)openSplash{
    //UIPageControl
    pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, HEIGHT-70, WITH,30 )];
    pageControl.backgroundColor=[UIColor grayColor];
    // 图片个数和点的个数相同.
    pageControl.numberOfPages = 4;
    // 其余圆点的颜色
    pageControl.pageIndicatorTintColor = [UIColor blueColor];
    // 被选中点的颜色
    pageControl.tag=1011;
    pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    
    scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WITH, HEIGHT)];
    //图片数组
    NSArray *strArrName=@[@"splash-1",@"splash-2",@"splash-3",@"splash-4"];
    scrollview.contentSize=CGSizeMake(WITH*4, HEIGHT);
    scrollview.contentOffset=CGPointMake(0, 0);
    scrollview.delegate=self;
    scrollview.scrollEnabled=YES;
    //隐藏水平滚动条
    scrollview.showsHorizontalScrollIndicator = NO;
    scrollview.showsVerticalScrollIndicator = NO;
    //去掉弹簧效果
    scrollview.bounces = NO;
    for(int i=0;i<[strArrName count];i++){
        NSString *imagename=strArrName[i];
        UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(WITH*i, 0, WITH, HEIGHT)];
        UIImage *image=[UIImage  imageNamed:imagename];
        [imageview setImage:image];
        [scrollview addSubview:imageview];
        
    }
    [self.view addSubview:scrollview];
    [self.view addSubview:pageControl];
    
    //立即体验
    UIButton *bt_enter =[[UIButton alloc]init];
    [bt_enter setBackgroundColor:[UIColor whiteColor]];
    CGFloat w=WITH*0.6;
    CGFloat x=WITH*3+(WITH-WITH*0.6)/2;
    CGFloat h=40;
    CGFloat y=HEIGHT-h*3;
    CGRect frame =  CGRectMake(x, y, w, h);
    [bt_enter setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [bt_enter setTitle:@"立即体验" forState:UIControlStateNormal];
    //高 h 一半圆角
    [bt_enter.layer setCornerRadius:h/2];
    bt_enter.layer.borderWidth=1;
    
    [bt_enter setFrame:frame];
    [bt_enter addTarget:self action:@selector(enterApp) forControlEvents:UIControlEventTouchUpInside];
    [scrollview addSubview:bt_enter];
}
-(void)enterApp{
    isFirst=@"1";
    [userdefalt setObject:isFirst forKey:@"isFirst"];
    [userdefalt synchronize];
    barController =[[UIMainBarViewController alloc]init];
    app= (AppDelegate *)[UIApplication sharedApplication].delegate;
    app.window.rootViewController=barController;
    [self enterMainControler];
}
-(void)enterMainControler{
    barController =[[UIMainBarViewController alloc]init];
    barController.viewControllers=@[newsNaCV,messageCV,readCV,mineCV,navtabview];
    
    app= (AppDelegate *)[UIApplication sharedApplication].delegate;
    app.window.rootViewController=barController;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
